package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Message;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class MessageService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieves the session (connection).
     */
    public MessageService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds Message to the db.
     * @param mes Message to be inserted
     * @return ID of the added Message
     */
    public int addMessage(Message mes) {
        session.beginTransaction();
        int id = getMaxMessageID() + 1;
        mes.setMessageID(id);
        id = (Integer) session.save(mes);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of Messages.
     * @return Max ID
     */
    public int getMaxMessageID() {
        String hql = "select max(m.messageID) from Message m";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxmesid = (Integer) list.get(0);
            return maxmesid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a Message identified by the ID.
     * @param id of the Message
     * @return Message with the ID given (null if not found)
     */
    public Message getMessageByID(int id) {
        session.beginTransaction();
        Message mes = (Message) session.get(Message.class, new Integer(id));
        session.getTransaction().commit();
        return (mes);
    }

    /**
     * Retrieves a list of all Messages stored in the db.
     * @return List of all Messages (null if no Messages)
     */
    public List<Message> getAllMessages() {
        session.beginTransaction();
        List<Message> mesList = session.createQuery("from Message").list();
        session.getTransaction().commit();
        return (mesList);
    }

    /**
     * Updates a specified Message identified by ID.
     * @param newMessage an updated Message
     */
    public void updateMessage(Message newMessage) {
        session.beginTransaction();
        Message mes = (Message) session.get(Message.class, new Integer(newMessage.getMessageID()));
        mes.setBody(newMessage.getBody());
        mes.setFrom(newMessage.getFrom());
        mes.setTo(newMessage.getTo());
        mes.setSubject(newMessage.getSubject());
        mes.setUnread(newMessage.isUnread());
        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the Message from db.
     * @param id Message ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteMessage(int id) {
        try {
            session.beginTransaction();
            Message mes = (Message) session.get(Message.class, new Integer(id));
            session.delete(mes);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}